﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VoteUphold
{
    /// <summary>
    /// 随机生成随机用户名，密码，邮箱，电话，QQ号，性别，年龄,姓名，身份证号码
    /// 单例
    /// </summary>
    public class RandData : IRandData
    {
        /// <summary>
        /// 字母 数字
        /// </summary>
        private  const string strWord = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

        private Random rand;

        private static RandData randData;

        /// <summary>
        /// 获取 对象 (饿汉式)
        /// </summary>
        /// <returns></returns>
        public static RandData GetInstance() { return randData; }


        static RandData()
        {
            randData = new RandData();
        }
        private RandData()
        {
            rand = new Random();
          
        }
        /// <summary>
        /// 生成用户名
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public string GetUsersName(int length)
        {
            return GetRandStr(length);
        }
        /// <summary>
        /// 生成密码
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public string GetPwd(int length)
        {
            return GetRandStr(length);
        }

        //public string GetIP()
        //{
        //    string str = "";
        //    for (int i = 0; i < 4; i++)
        //    {
        //        str += rd.Next(250) + ".";
        //    }
        //    str = str.Substring(0, str.Length - 1);
        //    return str;
        //}

        /// <summary>
        /// 生成邮箱
        /// </summary>
        /// <returns></returns>
        public string GetEmail(int length)
        {
            string yu = "";
            switch (rand.Next(3))
            {
                case 0: yu = "163"; break;
                case 1: yu = "qq"; break;
                case 2: yu = "sina"; break;
                default: yu = "yahu"; break;
            }
            switch (rand.Next(3))
            {
                case 0: yu += ".com"; break;
                case 1: yu += ".cn"; break;
                case 2: yu += ".net"; break;
                default: yu += ".com"; break;
            }
            return GetRandStr(length) + "@" + yu;
        }


        /// <summary>
        /// 获得电话号码
        /// </summary>
        /// <param name="areaCodeNum">电话号码的第4到8位</param>
        /// <returns></returns>
        public string GetTelphone(string areaCodeNum = null)
        {
            string str = "";

            int i = rand.Next(100);
            if (i < 40)
            {
                str = GetTelphone13();
            }
            else if (i >= 40 && i < 80)
            {
                str = getTelphone136_9_0733(areaCodeNum);
            }
            else if (i >= 80 && i < 90)
            {
                str = getTelphone187();
            }
            else if (i >= 90 && i < 100)
            {
                str = getTelphone151();
            }
            else
            {
                str = getTelphone136_9_0733();
            }

            if (str.Length != 11)
            {
                str = this.GetTelphone();
            }
            // str = "13807330123";
            return str;

        }
       

        /// <summary>
        /// 获得性别
        /// </summary>
        /// <returns></returns>
        public Gender GetSex()
        {
            Gender gender;
            int temp = rand.Next(2);
            if (temp == 1)
            {
                gender = Gender.男;
            }
            else
            {
                gender = Gender.女;
            }
            return gender;
        }

        /// <summary>
        /// 获得年龄
        /// 18-34之间
        /// </summary>
        /// <returns></returns>
        public int GetAge()
        {

            int age = 0;
            while (age < 18)
            {
                age = rand.Next(30);
            }
            return age;
        }

        /// <summary>
        /// 生成QQ
        /// </summary>
        /// <returns></returns>
        public string GetQQ()
        {
            int max = this.rand.Next(10);
            while (max <= 6)
            {
                max = this.rand.Next(10);
            }
            string str = "";
            for (int i = 0; i < max; i++)
            {
                str += "" + rand.Next(10);
            }
            if (str.Substring(0, 1) == "0")
            {
                str = "";
            }
            return str;

        }

        /// <summary>
        ///  随机 休息秒数
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public int GetSleepSeconds(int minSleep, int maxSleep)
        {
            return this.rand.Next(minSleep, maxSleep);
        }

        /// <summary>
        ///  随机 休息秒数
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public int GetSleepSeconds(int sleep)
        {
            return this.rand.Next(sleep);
        }

        /// <summary>
        /// 获取身份证号码
      /// </summary>
      /// <returns></returns>
        public string GetIDCard()
        {
            string value = null;
           return this.GetIDCard(out value);
        }
        /// <summary>
        /// 获取身份证号码
        /// </summary>
        /// <param name="value">此身份证号码的所在地区</param>
        /// <returns>返回正确的身份号码</returns>
        public string GetIDCard(out string value)
        {
           return RandIDCard.GetInstance().GetIDCard(out value);
        }

        /// <summary>
        /// 生成姓名
        /// </summary>
        /// <returns></returns>
        public string GetName(int? type = null)
        {
            return ChineseName.GetInstance().GetName(type.HasValue ? type.Value : -1);
        }

        /// <summary>
        /// 随机生成
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        private string GetRandStr(int length)
        {
            StringBuilder sb = new StringBuilder(length);
            for (int i = 0; i < length; i++)
            {
                sb.Append(strWord.Substring(rand.Next(strWord.Length), 1));
            }
            return sb.ToString();
        }

        /// <summary>
        /// 13-----
        /// </summary>
        /// <returns></returns>
        private string GetTelphone13()
        {
            string str = "13";
            for (int i = 0; i < 9; i++)
            {
                str += "" + rand.Next(10);
            }
            return str;
        }

        /// <summary>
        /// 1360733
        /// 1370733
        /// 1380733
        /// 1390733
        /// </summary>
        /// <returns></returns>
        private string getTelphone136_9_0733(string areaCodeNum = null)
        {

            string str = "13";
            int i = rand.Next(10);
            while (i < 5)
            {
                i = rand.Next(10);
            }
            str += i + (!string.IsNullOrEmpty(areaCodeNum) && areaCodeNum.Length == 4 ? areaCodeNum : "0733");
            for (i = 0; i < 4; i++)
            {
                str += rand.Next(10);
            }
            return str;

        }

        private string getTelphone187()
        {
            string str = "187";
            for (int i = 0; i < 8; i++)
            {
                str += "" + rand.Next(10);
            }
            return str;

        }

        private string getTelphone151()
        {
            string str = "151";
            for (int i = 0; i < 8; i++)
            {
                str += "" + rand.Next(10);
            }
            return str;

        }
    }
}
